package com.xinqi.modules.chat.chat.official.service.impl;

import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.core.constant.ChatRemindConstants;
import com.xinqi.modules.chat.chat.official.convert.ChatRemindConfigConvert;
import com.xinqi.modules.chat.chat.official.domain.ChatRemindConfigEntity;
import com.xinqi.modules.chat.chat.official.dto.req.ChatRemindConfigUpdateDTO;
import com.xinqi.modules.chat.chat.official.dto.rsp.ChatRemindConfigResultDTO;
import com.xinqi.modules.chat.chat.official.manager.ChatRemindConfigManager;
import com.xinqi.modules.chat.chat.official.service.ChatRemindConfigService;
import com.xinqi.modules.chat.third.rongyun.service.RongyunService;
import io.rong.util.CodeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * 新奇提醒通知设置表服务层实现
 *
 * @author: yyzhen
 * @date: 2023/05/05
 */
@Service("chatRemindConfigService")
@RequiredArgsConstructor
public class ChatRemindConfigServiceImpl implements ChatRemindConfigService {

    private final ChatRemindConfigConvert chatRemindConfigConvert;
    private final ChatRemindConfigManager chatRemindConfigManager;

    private final RongyunService rongyunService;

    /**
     * 查看新奇提醒通知设置
     *
     * @param userId
     * @return
     */
    @Override
    public ChatRemindConfigResultDTO infoConfig(Long userId) {
        ChatRemindConfigEntity configEntity = chatRemindConfigManager.selectByUserId(userId);
        if (Objects.isNull(configEntity)) {
            //没有则初始化
            chatRemindConfigManager.initByUserId(userId);
            configEntity = chatRemindConfigManager.selectByUserId(userId);
        }
        return chatRemindConfigConvert.convert(configEntity);
    }

    /**
     * 修改新奇提醒通知设置
     *
     * @param dto
     * @return
     */
    @Override
    public ChatRemindConfigResultDTO editConfig(ChatRemindConfigUpdateDTO dto) {
        ChatRemindConfigEntity old = chatRemindConfigManager.selectByUserId(dto.getOperationUserId());
        if (Objects.isNull(old)) {
            //没有则初始化
            chatRemindConfigManager.initByUserId(dto.getOperationUserId());
            old = chatRemindConfigManager.selectByUserId(dto.getOperationUserId());
        }
        //免打扰
        if (Objects.nonNull(dto.getIsDisturb())) {
            if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getIsDisturb())) {
                boolean muteFlag = rongyunService.conversationDisturb(dto.getOperationUserId().toString(), ChatRemindConstants.REMIND,
                    CodeUtil.ConversationType.PRIVATE, YesNoNumberEnum.YES.getCode());
                if (!muteFlag) {
                    dto.setIsDisturb(YesNoNumberEnum.NO.getCode());
                }
            } else if (Objects.equals(YesNoNumberEnum.NO.getCode(), dto.getIsDisturb())) {
                boolean unMuteFlag = rongyunService.conversationDisturb(dto.getOperationUserId().toString(), ChatRemindConstants.REMIND,
                    CodeUtil.ConversationType.PRIVATE, YesNoNumberEnum.NO.getCode());
                if (!unMuteFlag) {
                    dto.setIsDisturb(YesNoNumberEnum.NO.getCode());
                }
            }
        }
        ChatRemindConfigEntity configEntity = chatRemindConfigConvert.update(dto);
        configEntity.setUserId(dto.getOperationUserId());
        configEntity.setId(old.getId());
        chatRemindConfigManager.updateById(configEntity);
        configEntity = chatRemindConfigManager.selectByUserId(dto.getOperationUserId());
        return chatRemindConfigConvert.convert(configEntity);
    }
}
